package sctu.edu.java.year2016.team02.project.service.problem;

import sctu.edu.java.year2016.team02.project.Problem;
import sctu.edu.java.year2016.team02.project.service.SQLOperate;

import java.net.Socket;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by Eoly on 2016/6/11.
 */
public class ServerProblem implements Problem{
    private String mainMessage;
    private String detailMessage;
    private String solveResult;
    private String ipAddress;
    private String userNameAndIpAddress;
    private String keyToRE;
    private String tipMessage;


    public static final int SELECT = 4;
    public static final int DELETE = 5;
    public static final int UPDATE_STRING = 6;
    public static final int UPDATE_INT = 7;
    public static final int INSERT = 8;
    public static final int INSERT_KEY = 9;

    public ServerProblem(Socket socket) {
        ipAddress = socket.getInetAddress().toString();
    }

    @Override
    //调用数据库操作函数提交数据
    public void inHand() throws SQLException {
        userNameAndIpAddress =  ipAddress;
        keyToRE = userNameAndIpAddress + mainMessage;
        String[] name ={"UserNameAndIpAddress","KeyToRE","MainMessage","DetailMessage"};
        String[] value ={userNameAndIpAddress, keyToRE,mainMessage,detailMessage};
        if(!SQLOperate.insert("Problem",name,value,1)){
            tipMessage = "该内容及关键字重复添加！";
            System.out.println(tipMessage);
        }else {
            tipMessage = "提交成功！";
            System.out.println(tipMessage);
        }
    }

    @Override
    //调用数据库操作函数搜索数据
    public List<Object[]> seeking() throws SQLException {
        String[] filename = {"MainMessage","DetailMessage"};
        String[] value = {mainMessage,mainMessage};
        return SQLOperate.select("Problem",filename,value,true);
    }

    @Override
    //调用数据库操作函数搜索数据，有三个选择
    public List<Object[]> seeking( int option) throws SQLException {
        if(option!=2) {
            String[] filename = {"SolvedOrNot"};
            String[] value = {String.valueOf(option)};
            return SQLOperate.select("Problem",filename,value,true);
        }else {
            String[] filename = {""};
            return SQLOperate.select("Problem", filename, filename, true);
        }

    }

    public String getTipMessage() {
        return tipMessage;
    }

    public String getMainMessage() {
        return mainMessage;
    }

    public void setMainMessage(String mainMessage) {
        this.mainMessage = mainMessage;
    }

    public String getDetailMessage() {
        return detailMessage;
    }

    public void setDetailMessage(String detailMessage) {
        this.detailMessage = detailMessage;
    }

    public String getSolveResult() {
        return solveResult;
    }

    public void setSolveResult(String solveResult) {
        this.solveResult = solveResult;
    }
}
